import { Composer as l } from './compose/composer.js';
import { Document as m } from './doc/Document.js';
import { prettifyError as c, YAMLParseError as d } from './errors.js';
import { warn as p } from './log.js';
import { LineCounter as g } from './parse/line-counter.js';
import { Parser as a } from './parse/parser.js';
function u(t) {
  const e = t.prettyErrors !== !1;
  return {
    lineCounter: t.lineCounter || (e && new g()) || null,
    prettyErrors: e,
  };
}
function D(t, e = {}) {
  const { lineCounter: r, prettyErrors: s } = u(e),
    n = new a(r == null ? void 0 : r.addNewLine),
    f = new l(e),
    o = Array.from(f.compose(n.parse(t)));
  if (s && r)
    for (const i of o) i.errors.forEach(c(t, r)), i.warnings.forEach(c(t, r));
  return o.length > 0 ? o : Object.assign([], { empty: !0 }, f.streamInfo());
}
function y(t, e = {}) {
  const { lineCounter: r, prettyErrors: s } = u(e),
    n = new a(r == null ? void 0 : r.addNewLine),
    f = new l(e);
  let o = null;
  for (const i of f.compose(n.parse(t), !0, t.length))
    if (!o) o = i;
    else if (o.options.logLevel !== 'silent') {
      o.errors.push(
        new d(
          i.range.slice(0, 2),
          'MULTIPLE_DOCS',
          'Source contains multiple documents; please use YAML.parseAllDocuments()',
        ),
      );
      break;
    }
  return s && r && (o.errors.forEach(c(t, r)), o.warnings.forEach(c(t, r))), o;
}
function v(t, e, r) {
  let s;
  typeof e == 'function'
    ? (s = e)
    : r === void 0 && e && typeof e == 'object' && (r = e);
  const n = y(t, r);
  if (!n) return null;
  if (
    (n.warnings.forEach(f => p(n.options.logLevel, f)), n.errors.length > 0)
  ) {
    if (n.options.logLevel !== 'silent') throw n.errors[0];
    n.errors = [];
  }
  return n.toJS(Object.assign({ reviver: s }, r));
}
function C(t, e, r) {
  let s = null;
  if (
    (typeof e == 'function' || Array.isArray(e)
      ? (s = e)
      : r === void 0 && e && (r = e),
    typeof r == 'string' && (r = r.length),
    typeof r == 'number')
  ) {
    const n = Math.round(r);
    r = n < 1 ? void 0 : n > 8 ? { indent: 8 } : { indent: n };
  }
  if (t === void 0) {
    const { keepUndefined: n } = r ?? e ?? {};
    if (!n) return;
  }
  return new m(t, s, r).toString(r);
}
export {
  v as parse,
  D as parseAllDocuments,
  y as parseDocument,
  C as stringify,
};
